﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CharitiesLib;

namespace Charities
{
    public partial class ChangePasswordDlg : Form
    {
        public bool is_loaded = false;
        public ChangePasswordDlg(int i,Form m )
        {
            this.MdiParent = m;
            InitializeComponent();
            fo = m;
            try
            {
                User u = ctx.Users.Single(c => c.Id == i);
                user = u;

            }
            catch (Exception)
            {

                MessageBox.Show("خطأ بالاتصال بقاعدة البيانات");
            }
        }
        Form fo = null;
        ModelContainer ctx = new ModelContainer();
        User user = new User();
        private void btn_Dispose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        private void btn_Ok_Click(object sender, EventArgs e)
        {
            string old = Hash(txb_OldPwd.Text.Trim());
            string newp = Hash(txb_newPWD.Text.Trim());
            string renew = Hash(txb_retypePWD.Text.Trim());
            if (newp == renew)
            {
                if (old == user.password)
                {
                    user.password = old;
                    user.UpdateDate = DateTime.Now;
                    user.UpdateUId = user.Id;
                    ctx.SaveChanges();
                    this.Dispose();
                }
                else
                {
                    MessageBox.Show("كلمة السر القديمة غير صحيحة ");
                }
            }
            else
            {
                MessageBox.Show("كلمتا المرور الجديدتان غير متطابقتان");
            }
        }
        public static string Hash(string value)
        {
            var sha = new System.Security.Cryptography.SHA1CryptoServiceProvider();
            byte[] hash = sha.ComputeHash(Encoding.ASCII.GetBytes(value));
            return BytesToHex(hash).ToLower();
        }

        private static string BytesToHex(byte[] bytes)
        {
            return String.Concat(Array.ConvertAll(bytes, x => x.ToString("X2")));
        }

        private void ChangePasswordDlg_Load(object sender, EventArgs e)
        {
            is_loaded = true;
            // add this form to window list
            ((Forms.FormMDI)fo).addFormToWindowsList(this);
        }

        private void ChangePasswordDlg_FormClosed(object sender, FormClosedEventArgs e)
        {
            is_loaded = false;
            // remove this form from windows list
            ((Forms.FormMDI)fo).removeFormFromWindowsList(this);
        }

        private void ChangePasswordDlg_Activated(object sender, EventArgs e)
        {
            // activate this form and add it windows list as activate one
            ((Forms.FormMDI)this.MdiParent).activateWindow(this);
        }

    }
}
